﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;


public class UIManager : MonoBehaviour
{

    public static UIManager Instance;
    // UI遮罩
    public Image maskImage;
    // 玩家数据相关UI
    public Text playerScoreText;
    public Text playerBestScoreText;
    public Text playerGoldText;

    // 倒计时
    public GameObject countNumGo;
    public Text countNumText;
    private int countNum;

    // 设置面板
    // 静音图片
    public Sprite muteSprite;
    // 音量图片
    public Sprite volSprite;
    public Image imgVol;
    // 设置面板对象
    public GameObject setPanelGo;
    // 帮助面板对象
    public GameObject helpPanelGo;
    // 增加火箭长度
    // 火箭已达到最长
    public GameObject longestGo;
    // 添加火箭长度
    public GameObject addLengthGo;
    // 添加火箭长度Btn
    public Button addLengthBtn;
    public Text nextLevelNeedText;
    public Rocket rocket;
    // 开始游戏
    public Button gameStartBtn;
    public Text startGameText;
    // 游戏结算
    public GameObject gameWinPanel;
    public GameObject gameLosePanel;
    // 游戏失败面板背景图片 带点凄凉的那个图
    public Image imgGameOverPanel;

    // Start is called before the first frame update
    void Start()
    {
        Instance = this;
        HideMask();
        countNum = 3;
        InitUI();
        //StartGame();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void ShowMask() {

        DOTween.To(() => maskImage.color, toColor => maskImage.color = toColor, new Color(0, 0, 0, 1), 2)
            .OnComplete(ExitSceneComplete);
        
    }

    public void ExitSceneComplete()
    {
        SceneManager.LoadScene(0);
    }

    public void HideMask()
    {
        DOTween.To(() => maskImage.color, toColor => maskImage.color = toColor, new Color(0, 0, 0, 0), 2);
    }

    public void InitUI()
    {
        ChangePlayerSccore(0);
        ChangePlayerBestScore(PlayerManager.instance.playerBestScore);
        ChangePlayerGoldNum(PlayerManager.instance.money);
        countNumText.text = countNum.ToString();
        JudgeGoldEnough();
        DOTween.To(() => startGameText.color, toColor => startGameText.color=toColor   , new Color(255, 255, 1), 0.74f)
            .SetLoops(-1,LoopType.Yoyo);
        startGameText.DOFade(1,0.7F).SetLoops(-1,LoopType.Yoyo);
    }


    public void ChangePlayerSccore(int changeValue) {
        playerScoreText.text = changeValue.ToString();
    }

    public void ChangePlayerBestScore(int changeValue) {
        playerBestScoreText.text = changeValue.ToString();
    }

    // 更改金币显示
    public void ChangePlayerGoldNum(int changeValue) {
        PlayerManager.instance.money += changeValue;
        playerGoldText.text = PlayerManager.instance.money.ToString();

    }

    public void ShowCountNumGo(bool ifShowGo) {
        countNumGo.SetActive(ifShowGo);
    }


    public void StartGame()
    {
        //gameStartBtn.interactable = false;
        gameStartBtn.gameObject.SetActive(false);
        rocket.PlayFlyEffect();
        ShowCountNumGo(true);
        InvokeRepeating("EndCountNum", 1, 1);
    }

    private void EndCountNum()
    {
        countNum--;
        countNumText.text = countNum.ToString();
        if (countNum<=0) {
            CancelInvoke();
            ShowCountNumGo(false);
            rocket.StartGame();
        }
    }


    // 清空存档
    public void ClearSave()
    {

    }

    public void SetVolume()
    {
        // 每次设置调大音量 0.2
        PlayerManager.instance.volume += 0.2f;
        // 当音量大于1的时候  设置为静音
        if (PlayerManager.instance.volume > 1)
        {
            // 静音设置音量大小为0
            PlayerManager.instance.volume = 0;
            // 设置音量图标为 静音图标
            imgVol.sprite = muteSprite;
            // 设置图标颜色（前三个参数是 设置颜色  最后一个是透明度）
            imgVol.color = new Color(1, 0, 0, 1f);
            // 设置缩放尺寸为保持原尺寸
            imgVol.transform.localScale = Vector3.one;
        }
        else
        {
            // 调整了原尺寸是因为那个正常声音的图标  比静音图标要小
            imgVol.color = new Color(1, 1, 1, PlayerManager.instance.volume);
            // 设置音量图标为非静音图标
            imgVol.sprite = volSprite;

        }
    }


    public void OpenSetPanel(bool open) {
        if (open)
        {
            setPanelGo.SetActive(true);
            setPanelGo.transform.DOScaleY(1, 0.5f);
        }
        else
        {
            setPanelGo.transform.DOScaleY(0, 0.5f).OnComplete(() =>
            {
                setPanelGo.SetActive(false);
            });
        }
    
    }

    public void OpenHelpPanel(bool open)
    {
        if (open)
        {
            helpPanelGo.SetActive(true);
            helpPanelGo.transform.DOScaleX(1, 0.5f);
        }
        else
        {
            helpPanelGo.transform.DOScaleX(0, 0.5f).OnComplete(() =>
            {
                helpPanelGo.SetActive(false);
            });
        }

    }

    // 增加火箭长度
    public void AddRocketLength()
    {
       rocket.AddRocketLength();
        // 减少金钱
       ChangePlayerGoldNum(-GetAddRocketLengthNeedMoney());
       JudgeGoldEnough();
    }
    public void JudgeGoldEnough()
    {
        if (PlayerManager.instance.rocketLength >= 10) { 
            longestGo.SetActive(true);
            addLengthGo.SetActive(false);

            return;
        }
        nextLevelNeedText.text = GetAddRocketLengthNeedMoney().ToString();
        //如果我们的金币 大于   增加下节火箭所需要的金额  我们的增加火箭长度的按钮 是可以交互的
        // 否则 增加火箭长度的按钮是不能交互的
        if (PlayerManager.instance.money >= GetAddRocketLengthNeedMoney()) {

            addLengthBtn.interactable = true;

        }
        else
        {
            addLengthBtn.interactable = false;
        }

    }

    public int GetAddRocketLengthNeedMoney()
    {
        return 50 + (PlayerManager.instance.rocketLength - 5) * 100;
    }


    public void OpenGameWinPanel()
    {
        gameWinPanel.SetActive(true);
    }

    public void OpenGameLosePanel()
    {
        gameLosePanel.SetActive(true);
        imgGameOverPanel.DOFade(1, 4);
    }
}
